package main;

import main.Link.LinkArray;
import main.Link.LinkTable;
import main.Trees.ProjectNode;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by s_shmakov on 07.05.14.
 */
public class FilterTaskPanel extends JPanel {

    public ArrayList<ProjectNode> filterList;
    private String forButton;
    private LinkTable linkTM;
    private JTable linkTable;
    private LinkArray linkTableArray;
    private JFrame linkFrame;
    private JMenuItem FilterMenuItem;

    public FilterTaskPanel(ArrayList<ProjectNode> filterList) {
        this.filterList = filterList;
    }

    public FilterTaskPanel() {

    }

    public JPanel headPanel() {
        add(new JLabel("Текущие связи по проекту"));
        return this;
    }

    public JPanel setFilterHead(ArrayList<ProjectNode> filterList, LinkTable linkTM, JTable linkTable, LinkArray linkTableArray, JFrame linkFrame) {
        this.removeAll();
//        System.out.println("filterList = " + filterList);
        this.filterList = filterList;
        this.linkTM = linkTM;
        this.linkTable = linkTable;
        this.linkTableArray = linkTableArray;
        this.linkFrame = linkFrame;
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        this.repaint();

        if (filterList != null) {
            for (int i = 0; i < filterList.size(); i++) {
                if (filterList.size() != 0) {
                    final JLabel buttonTask = new JLabel(filterList.get(i).getShortName());
                    forButton = filterList.get(i).getShortName();
                    buttonTask.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent mouseEvent) {
                            reFiltred(buttonTask.getText());
                        }
                    });

                    buttonTask.setOpaque(true);
                    buttonTask.setBorder(BorderFactory.createCompoundBorder(new LineBorder(new Color(114, 140, 216), 1, true), BorderFactory.createEmptyBorder(5,5,5,5)));
                    buttonTask.setBackground(new Color(253, 253, 253));
                    buttonTask.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                    try {
                        Image iconAdd = ImageIO.read(getClass().getResource("images/close.png"));
                        buttonTask.setIcon(new ImageIcon(iconAdd));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    c = new GridBagConstraints();
                    c.gridx = i - i/11 * 11;
                    c.gridy = i/11;
                    c.fill = GridBagConstraints.BOTH;
                    c.insets = new Insets(0, 0, 5, 10);
                    c.anchor = GridBagConstraints.WEST;

                    add(buttonTask, c);
                    revalidate();
                }

            }
        } else {
            JLabel headText = new JLabel("Текущие связи по проекту");
            headText.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
            add(headText);
        }

        revalidate();
        return this;
    }

    private void reFiltred(String task) {
        if (filterList.size() > 1) {
            for (int i = 0; i < filterList.size(); i++) {
                if (filterList.get(i).getShortName() == task) {
                    filterList.remove(i);
                }
            }
            linkTM.setData(linkTableArray.setTaskFilter(filterList));
            setFilterHead(filterList, linkTM, linkTable, linkTableArray, linkFrame);
            linkTable.repaint();
            linkFrame.revalidate();
            linkFrame.repaint();
        } else {
            FilterMenuItem.setText("Установить фильтр");
            filterList = null;
            linkFrame.setVisible(false);
            linkTM.setData(linkTableArray.getLinkArray());
            linkTableArray.setFilter(false);
            setFilterHead(filterList, linkTM, linkTable, linkTableArray, linkFrame);
        }

    }

    public void removeFilter() {
        filterList = null;
        setFilterHead(filterList, linkTM, linkTable, linkTableArray, linkFrame);
    }

    public boolean isNewTask(ArrayList<ProjectNode> list) {
        boolean res = false;
        HashMap<ProjectNode, Boolean> sushReestr = new HashMap<>();
        if (filterList != null) {
            for (ProjectNode fl : filterList) {
                sushReestr.put(fl, true);
            }
            for (ProjectNode l : list) {
                if (sushReestr.get(l) == null) {
                    //System.out.println("yes");
                    res = true;
                }
            }
        }
        return res;
    }

    public ArrayList<ProjectNode> updateFilter(ArrayList<ProjectNode> temp) {
        for (ProjectNode node : filterList) {
            temp.add(node);
        }
        return temp;
    }

    public void setFilterMenuItem(JMenuItem filterMenuItem) {
        this.FilterMenuItem = filterMenuItem;
    }
}
